www.gusucode.com > TinyShop PHP开源商城系统 v1.0.3PHP源码程序 > TinyShop PHP开源商城系统 v1.0.3/TinyShop_v1.0.3/TinyShop_v1.0.3/protected/controllers/simple.php

    <?php
/**
 * description...
 * 
 * @author Tinyhu
 * @package SimpleController
 */
class SimpleController extends Controller{
    
    public $layout='simple';
    public $safebox = null;
    private $user;
    private $model = null;
    private $cookie_time = 31622400;
    private $cart = array();

    public function init(){
        header("Content-type: text/html; charset=".$this->encoding);
        $this->model = new Model();
        $this->safebox =  Safebox::getInstance();
        $this->user = $this->safebox->get('user');
        if($this->user==null){
            $this->user = Common::autoLoginUserInfo();
            $this->safebox->set('user',$this->user);
        }
        $cart = Cart::getCart();
        $this->cart = $cart->all();
        $this->assign("cart",$this->cart);
    }

    public function reg_act(){
        $email = Filter::sql(Req::post('email'));
        $passWord = Req::post('password');
        $rePassWord = Req::post('repassword');
        $this->safebox = Safebox::getInstance();
        $code = $this->safebox->get($this->captchaKey);
        $verifyCode = Req::args("verifyCode");
        
        $info = array('field'=>'verifyCode','msg'=>'验证码错误!');
        if($verifyCode==$code){
            if($passWord == $rePassWord){
                $model = $this->model->table("user");
                $obj = $model->where("email='$email'")->find();
                if($obj==null){
                    $validcode = CHash::random(8);
                    $last_id = $model->data(array('email'=>$email,'name'=>$email,'password'=>CHash::md5($passWord,$validcode),'validcode'=>$validcode))->insert();
                    $time = date('Y-m-d H:i:s');
                    $model->table("customer")->data(array('user_id'=>$last_id ,'reg_time'=>$time,'login_time'=>$time))->insert();
                    //记录登录信息
                    $obj = $model->table("user as us")->join("left join customer as cu on us.id = cu.user_id")->fields("us.*,cu.group_id,cu.login_time")->where("us.email='$email'")->find();

                    $this->safebox->set('user',$obj,1800);

                    $this->redirect("reg_result");

                }
                else{
                    $info = array('field'=>'email','msg'=>'此用户已经被注册!');
                }
            }
            else{
                $info = array('field'=>'repassword','msg'=>'两次密码输入不一致!');
            }
        }
        $this->assign("invalid",$info);
        $this->redirect("reg",false,Req::args());
    }
    public function login()
    {
        if($this->checkOnline()) $this->redirect('/ucenter/index');
        else $this->redirect();
    }
    public function login_act(){
        $redirectURL = Req::args("redirectURL");
        $this->assign("redirectURL",$redirectURL);
        $email = Filter::sql(Req::post('email'));
        $passWord = Req::post('password');
        $autologin = Req::args("autologin");
        if($autologin==null)$autologin = 0;
        $model = $this->model->table("user as us");
        $obj = $model->join("left join customer as cu on us.id = cu.user_id")->fields("us.*,cu.group_id,cu.login_time")->where("us.email='$email'")->find();
        if($obj){
            if($obj['password'] == CHash::md5($passWord,$obj['validcode'])){
                $cookie = new Cookie();
                $cookie->setSafeCode(Tiny::app()->getSafeCode());
                if($autologin==1) {
                    $this->safebox->set('user',$obj,$this->cookie_time);
                    
                    $cookie->set('autologin',array('email'=>$email,'password'=>$obj['password']),$this->cookie_time);
                }
                else {
                    $cookie->set('autologin',null,0);
                    $this->safebox->set('user',$obj, 1800);

                }
                $this->model->table("customer")->data(array('login_time'=>date('Y-m-d H:i:s')))->where('user_id='.$obj['id'])->update();
                $redirectURL = Req::args("redirectURL");

                if($redirectURL!='' && stripos($redirectURL, "http://")===false && stripos($redirectURL, "reg")===false && stripos($redirectURL, "login_act")===false && stripos($redirectURL, "reset_password_act")===false)header('Location: '.$redirectURL, true, 302);
                else $this->redirect('/ucenter/index');
                exit;
            }else{
                $info = array('field'=>'password','msg'=>'密码错误!');
            }
        }else{
            $info = array('field'=>'email','msg'=>'账号不存在!');
        }
        $this->assign("invalid",$info);
        $this->redirect("login",false,Req::args());
    }
    public function forget_act(){
        $email =  Filter::sql(Req::args('email'));
        $model = $this->model->table('user');
        $obj = $model->where("email = '".$email."'")->find();
        if(!empty($obj)){
            $model = $this->model->table('reset_password');
            $obj = $model->where("email = '".$email."'")->find();
            $safecode = md5(md5($email).md5(time()));
            if(!empty($obj)){
                $obj['safecode'] = $safecode;
                $model->data($obj)->update();
            }
            else{
                $model->data(array('email'=>$email,'safecode'=>$safecode))->add();
            }
            

            $url = Url::getHost().Url::urlFormat("/simple/reset_password/safecode/$safecode");
            $html = '';
            $html .='<p>亲爱的用户:</p>';
            $html .='<p>感谢您注册'.$this->site_name.',请点击以下链接重置您的密码。<br/><br/>';
            $html .="<a href='{$url}' target='_blank'>{$url}</a><br/><br/>";
            $html .='愿您在'.$this->site_name.'度过愉快的时光。<br/><br/>';
            $html .="<a href='".Url::getHost().Url::urlFormat('/')."'>".$this->site_name."</a></p>";
            $mail = new Mail();
            $flag = $mail->send_email($email,'找回密码--'.$this->site_name,$html);
            if($flag){
                $this->assign('status','success');
            }
            else{
                $this->assign('status','error');
            }
        }else{
            $this->assign('status','fail');
        }
       $this->redirect('forget_result',false);
    }
    public function reset_password()
    {
        $safecode = Filter::sql(Req::args('safecode'));
        if($safecode!=null && strlen($safecode)==32)
        {
            $model = $this->model->table('reset_password');
            $obj = $model->where("safecode='".$safecode."'")->find();
            $this->assign('status','fail');
            $this->assign('safecode',$safecode);
            if(!empty($obj)) $this->assign('status','success');
            $this->redirect();
        }
        else
        {
            $this->redirect('index/index');
        }
    }
    public function reset_password_act(){
        $safecode = Filter::sql(Req::args('safecode'));
        $password = Req::args('password');
        $repassword = Req::args('repassword');
        if($password == $repassword)
        {
            $model = new Model('reset_password');
            $obj = $model->where("safecode='".$safecode."'")->find();
            if(!empty($obj))
            {
                $umodel = $this->model->table('user');
                $validcode = CHash::random(8);
                $umodel->where("email='".Filter::sql($obj['email'])."'")->data(array('password'=>CHash::md5($password,$validcode),'validcode'=>$validcode))->update();
                $model->where('id='.$obj['id'])->delete();
                $this->assign('status','success');
                $this->redirect('reset_result',false);
            }
            else
            {
                $this->assign('status','fail');
                $this->redirect('reset_result',false);
            }
        }
        else
        {
            $this->assign("invalid",array('field'=>'repassword','msg'=>'两次密码不一致!'));
            $this->redirect('reset_password',false,Req::args());
        }
    }
    public function order(){
        if(!$this->cart)$this->redirect("cart");
        if($this->checkOnline()){
            $this->parserOrder();
            $this->redirect();
        }else{
            $this->redirect("login");
        }
    }
    //解析订单
    private function parserOrder(){
        $config = Config::getInstance();
        $config_other = $config->get('other');
        $is_invoice = isset($config_other['other_is_invoice'])?!!$config_other['other_is_invoice']:false;
        $tax = isset($config_other['other_tax'])?intval($config_other['other_tax']):0;

        $area_ids = array();
        $address = $this->model->table("address")->where("user_id=".$this->user['id'])->order("is_default desc")->findAll();
        foreach ($address as $add) {
            $area_ids[$add['province']] = $add['province'];
            $area_ids[$add['city']] = $add['city'];
            $area_ids[$add['county']] = $add['county'];
        }
        $area_ids = implode(",",$area_ids);
        $areas = array();
        if($area_ids!='')$areas = $this->model->table("area")->where("id in($area_ids )")->findAll();
        $parse_area = array();
        foreach ($areas as $area) {
            $parse_area[$area['id']] = $area['name'];
        }

        $model = new Model("voucher");
        $where = "user_id = ".$this->user['id']." and is_send = 1";
        $where .= " and status = 0 and '".date("Y-m-d H:i:s")."' <=end_time";
        $voucher = $model->where($where)->order("id desc")->findAll();

        $this->assign("voucher",$voucher);
        $this->assign("is_invoice",$is_invoice);
        $this->assign("tax",$tax);
        $this->assign("address",$address);
        $this->assign("parse_area",$parse_area);
        $this->assign("order_status",Session::get("order_status"));
    }
    //打包团购订单商品信息
    private function packGroupbuyProducts($item,$num=1){
        $store_nums = $item['store_nums'];
        $have_num = $item['max_num']-$item['goods_num'];
        if($have_num>$store_nums) $have_num = $store_nums;
        if($num>$have_num) $num = $have_num;
        $amount = sprintf("%01.2f",$item['price']*$num);
        $sell_total = $item['sell_price']*$num;
        $product_id = $item['product_id'];

        $product[$product_id] = array('id'=>$product_id,'goods_id'=>$item['goods_id'],'name'=>$item['name'],'img'=>$item['img'],'num'=>$num,'store_nums'=>$have_num,'price'=>$item['price'],'spec'=>unserialize($item['spec']),'amount'=>$amount,'sell_total'=>$sell_total,'weight'=>$item['weight'],'point'=>$item['point'],"prom_goods"=>array(),"sell_price"=>$item['sell_price'],"real_price"=>$item['price']);
        return $product;
    }
    //打包抢购订单商品信息
    private function packFlashbuyProducts($item,$num=1){
        $store_nums = $item['store_nums'];
        $quota_num = $item['quota_num'];
        $have_num = $item['max_num']-$item['goods_num'];
        if($have_num>$store_nums) $have_num = $store_nums;
        if($have_num>$quota_num) $have_num = $quota_num;
        if($num>$have_num) $num = $have_num;
        $amount = sprintf("%01.2f",$item['price']*$num);
        $sell_total = $item['sell_price']*$num;
        $product_id = $item['product_id'];

        $product[$product_id] = array('id'=>$product_id,'goods_id'=>$item['goods_id'],'name'=>$item['name'],'img'=>$item['img'],'num'=>$num,'store_nums'=>$have_num,'price'=>$item['price'],'spec'=>unserialize($item['spec']),'amount'=>$amount,'sell_total'=>$sell_total,'weight'=>$item['weight'],'point'=>$item['point'],"prom_goods"=>array(),"sell_price"=>$item['sell_price'],"real_price"=>$item['price']);
        return $product;
    }
    //捆绑订单商品信息
    private function packBundbuyProducts($items,$num=1){
        $max_num = $num;
        foreach ($items as $prod) if($max_num>$prod['store_nums'])$max_num = $prod['store_nums'];
        $num = $max_num;
        foreach($items as $item) {
            $store_nums = $item['store_nums'];
            $amount = sprintf("%01.2f",$item['sell_price']*$num);
            $sell_total = $item['sell_price']*$num;
            $product_id = $item['product_id'];

            $product[$product_id] = array('id'=>$product_id,'goods_id'=>$item['goods_id'],'name'=>$item['name'],'img'=>$item['img'],'num'=>$num,'store_nums'=>$item['store_nums'],'price'=>$item['sell_price'],'spec'=>unserialize($item['spec']),'amount'=>$amount,'sell_total'=>$sell_total,'weight'=>$item['weight'],'point'=>$item['point'],"prom_goods"=>array(),"sell_price"=>$item['sell_price'],"real_price"=>$item['sell_price']);
        }
        return $product;
    }
    //非普通促销确认订单
    public function order_info(){
        $id = Filter::int(Req::args('id'));
        $product_id = Req::args('pid');
        $type = Req::args("type");
        if($this->checkOnline()){
            if($type=='groupbuy'){
            	$product_id = Filter::int($product_id);
                $model = new Model("groupbuy as gb");
                $item = $model->join("left join goods as go on gb.goods_id=go.id left join products as pr on pr.goods_id=gb.goods_id")->fields("*,pr.id as product_id,pr.store_nums")->where("gb.id=$id and pr.id=$product_id")->find();
                if($item){
                    $start_diff = time()-strtotime($item['start_time']);
                    $end_diff = time()-strtotime($item['end_time']);
                    if($item['is_end']==0 && $start_diff>=0 && $end_diff<0 && $item['store_nums']>0){
                        $product = $this->packGroupbuyProducts($item);
                        $this->assign("product",$product);
                    }else{
                        $this->redirect("/index/groupbuy/id/$id");
                    }
                }else{
                    Tiny::Msg($this,"你提交的团购不存在!",404);
                    exit;
                }
            }else if($type=='flashbuy'){
                $model = new Model("flash_sale as fb");
                $product_id = Filter::int($product_id);
                $item = $model->join("left join goods as go on fb.goods_id=go.id left join products as pr on pr.goods_id=fb.goods_id")->fields("*,pr.id as product_id,pr.store_nums")->where("fb.id=$id and pr.id=$product_id")->find();
                if($item){
                    $start_diff = time()-strtotime($item['start_time']);
                    $end_diff = time()-strtotime($item['end_time']);
                    if($item['is_end']==0 && $start_diff>=0 && $end_diff<0 && $item['store_nums']>0){
                        $product = $this->packFlashbuyProducts($item);
                        $this->assign("product",$product);
                    }else{
                        $this->redirect("/index/flashbuy/id/$id");
                    }
                }else{
                    Tiny::Msg($this,"你提交的抢购不存在!",404);
                    exit;
                }
                
            }else if($type == 'bundbuy'){
                //确认捆绑存在有效且所有的商品都在其中包括个数完全正确
                $product_id = trim($product_id,"-");
                $product_id_array = explode("-",$product_id);
                foreach($product_id_array as $key=>$val){
                	$product_id_array[$key] = Filter::int($val);
                }
                $product_ids = implode(',', $product_id_array);
                $model = new Model("bundling");
                $bund = $model->where("id=$id")->find();
                if($bund){
                    $goods_id_array = explode(',',$bund['goods_id']);

                    $products = $model->table("goods as go")->join("left join products as pr on pr.goods_id=go.id")->where("pr.id in ($product_ids)")->fields("*,pr.id as product_id")->group("go.id")->findAll();
                    //检测库存与防偷梁换柱
                    foreach ($products as  $value) {
                        if($value['store_nums']<=0 || !in_array($value['goods_id'], $goods_id_array)){
                            $this->redirect("/index/bundbuy/id/$id");
                        }
                    }
                    if(count($goods_id_array)==count($products)){
                        $product = $this->packBundbuyProducts($products);
                        $this->assign("product",$product);
                        $this->assign("bund",$bund);
                    }else{
                        $this->redirect("/index/bundbuy/id/$id");
                    }
                    $product_id = $product_id;
                }
                else{
                   $this->redirect("/index/msg",true,array('msg'=>'你提交的套餐不存在!','type'=>'error')); 
                }
            }
            $this->assign("id",$id);
            $this->assign("order_type",$type);
            $this->assign("pid",$product_id);
            $this->parserOrder();
            $this->redirect();
        }else{
            $this->redirect("login");
        }
    }
    //团购商品数量
    public function groupbuy_num(){
        $id = Filter::int(Req::args('id'));
        $num = Filter::int(Req::args('num'));
        if($num<=0)$num = 1;
        $product_id = Filter::int(Req::args('pid'));
        $model = new Model("groupbuy as gb");
        $item = $model->join("left join goods as go on gb.goods_id=go.id left join products as pr on pr.id=$product_id")->fields("*,pr.id as product_id")->where("gb.id=$id")->find();
        $product = $this->packGroupbuyProducts($item,$num);
        echo JSON::encode($product);
    }
    //抢购商品数量
    public function flashbuy_num(){
        $id = Filter::int(Req::args('id'));
        $num = Filter::int(Req::args('num'));
        if($num<=0)$num = 1;
        $product_id = Filter::int(Req::args('pid'));
        $model = new Model("flash_sale as fb");
        $item = $model->join("left join goods as go on fb.goods_id=go.id left join products as pr on pr.id=$product_id")->fields("*,pr.id as product_id")->where("fb.id=$id")->find();
        $product = $this->packFlashbuyProducts($item,$num);
        echo JSON::encode($product);
    }
    //捆绑商品数量
    public function bundbuy_num(){
        $id = Filter::int(Req::args('id'));
        $num = Filter::int(Req::args('num'));
        if($num<=0)$num = 1;
        $product_id = Req::args('pid');
        $product_ids = preg_replace('/-/i', ',', $product_id);
       
        $model = new Model("bundling");
        $bund = $model->where("id=$id")->find();
        if($bund){
            $goods_id = $bund['goods_id'];
            $products = $model->table("goods as go")->join("left join products as pr on pr.goods_id=go.id")->where("pr.id in ($product_ids)")->fields("*,pr.id as product_id")->group("go.id")->findAll();
            $products = $this->packBundbuyProducts($products);
        }
        $weight = 0;
        $max_num = $num;
        foreach ($products as $prod) {
            $weight += $prod['weight'];
            if($max_num>$prod['store_nums'])$max_num = $prod['store_nums'];
        }
        $num = $max_num;
        $amount = sprintf("%01.2f",$bund['price'] * $num);
        $product[$product_id] = array('id'=>$product_ids,'goods_id'=>'','name'=>'','img'=>'','num'=>$num,'store_nums'=>$num,'price'=>$bund['price'],'spec'=>array(),'amount'=>$amount,'sell_total'=>$amount,'weight'=>$weight,'point'=>'',"prom_goods"=>array(),"sell_price"=>$bund['price'],"real_price"=>$bund['price']);
        echo JSON::encode($product);
    }
    //提交订单处理
    public function order_act(){
        if($this->checkOnline()){
            $address_id = Filter::int(Req::args('address_id'));
            $payment_id = Filter::int(Req::args('payment_id'));
            $prom_id = Filter::int(Req::args('prom_id'));
            $is_invoice = Filter::int(Req::args('is_invoice'));
            $invoice_type = Filter::int(Req::args('invoice_type'));
            $invoice_title = Filter::int(Req::args('invoice_title'));
            $user_remark = Filter::txt(Req::args('user_remark'));
            $voucher_id = Filter::int(Req::args('voucher'));

            //非普通促销信息
            $type = Req::args("type");
            $id = Filter::int(Req::args('id'));
            $product_id = Req::args('product_id');
            $buy_num = Req::args('buy_num');

            if(!$address_id || !$payment_id){
                if(is_array($product_id)){
                	foreach($product_id as $key=>$val){
                		$product_id[$key] = Filter::int($val);
                	}
                	$product_id = implode('-', $product_id);
                }
                else $product_id =  Filter::int($product_id);
                $data = Req::args();
                if(!$address_id) $data['msg'] = array('fail',"必需选择收货地址,才能确认订单。");
                else $data['msg'] = array('fail',"必需选择支付方式,才能确认订单。");
                if($type==null)$this->redirect("order",false,$data);
                else {
                    unset($data['act']);
                    Req::args('pid',$product_id);
                    Req::args('id',$id);
                    unset($_GET['act']);
                    Req::args('type',$type);
                    Req::args('msg',$data['msg']);
                    $this->redirect("/simple/order_info",true,Req::args());
                }
                exit;
            }
            //订单类型: 0普通订单 1团购订单 2限时抢购 3捆绑促销
            $order_type = 0;
            $model = new Model('');

            //团购处理
            if($type=="groupbuy"){
                $product_id = Filter::int($product_id[0]);
                $num = $buy_num[0];
                $item = $model->table("groupbuy as gb")->join("left join goods as go on gb.goods_id=go.id left join products as pr on pr.id=$product_id")->fields("*,pr.id as product_id,pr.spec")->where("gb.id=$id")->find();
                $order_products = $this->packGroupbuyProducts($item,$num);

                $groupbuy = $model->table("groupbuy")->where("id=$id")->find();
                unset($groupbuy['description']);
                $data['prom'] = serialize($groupbuy);
                $data['prom_id'] = $id;
                $order_type = 1;

            }else if($type=="flashbuy"){//抢购处理
                $product_id = Filter::int($product_id[0]);
                $num = $buy_num[0];
                $item = $model->table("flash_sale as fb")->join("left join goods as go on fb.goods_id=go.id left join products as pr on pr.id=$product_id")->fields("*,pr.id as product_id,pr.spec")->where("fb.id=$id")->find();
                $order_products = $this->packFlashbuyProducts($item,$num);

                $flashbuy = $model->table("flash_sale")->where("id=$id")->find();
                unset($flashbuy['description']);
                $data['prom'] = serialize($flashbuy);
                $data['prom_id'] = $id;
                $order_type = 2;
            }else if($type=="bundbuy"){//捆绑销售处理
                $product_ids = implode(',', $product_id);
                $num = Filter::int($buy_num[0]);

                $model = new Model("bundling");
                $bund = $model->where("id=$id")->find();
                if($bund){
                    $goods_id = $bund['goods_id'];
                    $products = $model->table("goods as go")->join("left join products as pr on pr.goods_id=go.id")->where("pr.id in ($product_ids)")->fields("*,pr.id as product_id,pr.spec")->group("go.id")->findAll();
                    $order_products = $this->packBundbuyProducts($products,$num);
                }
                
                $bundbuy = $model->table("bundling")->where("id=$id")->find();
                unset($bundbuy['description']);
                $data['prom'] = serialize($bundbuy);
                $data['prom_id'] = $id;
                $current = current($order_products);
                $bundbuy_amount = sprintf("%01.2f",$bund['price']) * $current['num'];
                
                $order_type = 3;
            }
            if($order_type==0){
                $order_products = $this->cart;
                $data['prom_id'] = $prom_id;
            }
            
            //地址信息
            $address = $model->table("address")->where("id=$address_id")->find();
            //if(!$address)$this->redirect("order",false,Req::args());
            //if(!$payment_id)$this->redirect("order",false,Req::args());    

            //商品总金额,重量,积分计算
            $payable_amount = 0.00;
            $real_amount = 0.00;
            $weight=0;
            $point = 0;
            foreach ($order_products as $item) {
                $payable_amount+=$item['sell_total'];
                $real_amount+=$item['amount'];
                $weight += $item['weight']*$item['num'];
                $point += $item['point']*$item['num'];
            }
            if($order_type == 3) $real_amount = $bundbuy_amount;

            //计算运费
            $fare = new Fare($weight);
            $payable_freight = $fare->calculate($address_id);
            $real_freight = $payable_freight;

            //计算订单优惠
            $prom_order = array();
            $discount_amount = 0;
            if($order_type ==0 ){
                if($prom_id){
                    $prom = new Prom($real_amount);
                    $prom_order = $model->table("prom_order")->where("id=$prom_id")->find();

                    //防止非法会员使用订单优惠
                    $user = $this->user;
                    $group_id = ',0,';
                    if(isset($user['group_id'])) $group_id = ','.$user['group_id'].',';

                    if(stripos(','.$prom_order['group'].',',$group_id)!==false){
                        $prom_parse = $prom->parsePorm($prom_order);
                        $discount_amount = $prom_parse['value'];
                        if($prom_order['type']==4) $discount_amount = $payable_freight;
                        else if($prom_order['type']==2){
                            $multiple = intval($prom_order['expression']);
                            $multiple = $multiple==0?1:$multiple;
                            $point = $point * $multiple;
                        }
                        $data['prom'] = serialize($prom_order);
                    }
                    else $data['prom'] = serialize(array());
                } 
            }
            //税计算
            $tax_fee = 0;
            $config = Config::getInstance();
            $config_other = $config->get('other');
            $open_invoice = isset($config_other['other_is_invoice'])?!!$config_other['other_is_invoice']:false;
            $tax = isset($config_other['other_tax'])?intval($config_other['other_tax']):0;
            if($open_invoice && $is_invoice){
                $tax_fee = $real_amount*$tax/100;
            }

            //代金券处理
            $voucher_value = 0;
            $voucher = array();
            if($voucher_id){
                $voucher = $model->table("voucher")->where("id=$voucher_id and is_send=1 and user_id=".$this->user['id']." and status = 0 and '".date("Y-m-d H:i:s")."' <=end_time and '".date("Y-m-d H:i:s")."' >=start_time and money<=".$real_amount)->find();
                if($voucher){
                    $voucher_value = $voucher['value'];
                    if($voucher_value>$real_amount)$voucher_value = $real_amount;
                }
            }
            //计算订单总金额
            $order_amount = $real_amount + $payable_freight + $tax_fee - $discount_amount - $voucher_value;

            

            //填写订单
            $data['order_no'] = Common::createOrderNo();
            $data['user_id'] = $this->user['id'];
            $data['payment'] = $payment_id;
            $data['status'] = 2;
            $data['pay_status'] = 0;
            $data['accept_name'] = $address['accept_name'];
            $data['phone'] = $address['phone'];
            $data['mobile'] = $address['mobile'];
            $data['province'] = $address['province'];
            $data['city'] = $address['city'];
            $data['county'] = $address['county'];
            $data['addr'] = $address['addr'];
            $data['zip'] = $address['zip'];
            $data['payable_amount'] = $payable_amount;
            
            $data['payable_freight'] = $payable_freight;
            $data['real_freight'] = $real_freight;
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['user_remark'] = $user_remark;
            $data['is_invoice'] = $is_invoice;
            $data['invoice_title'] = $invoice_title;
            $data['taxes'] = $tax_fee;
            
            
            $data['discount_amount'] = $discount_amount;

            $data['order_amount'] = $order_amount;
            $data['real_amount'] = $real_amount;
            
            $data['point'] = $point;
            $data['type'] = $order_type;
            $data['voucher_id'] = $voucher_id;
            $data['voucher'] = serialize($voucher);


            //var_dump($order_products);exit();

            //写入订单数据
            $order_id = $model->table("order")->data($data)->insert();
            //写入订单商品
            $tem_data = array();

            foreach ($order_products as $item) {
                $tem_data['order_id'] = $order_id;
                $tem_data['goods_id'] = $item['goods_id'];
                $tem_data['product_id'] = $item['id'];
                $tem_data['goods_price'] = $item['sell_price'];
                $tem_data['real_price'] = $item['real_price'];
                $tem_data['goods_nums'] = $item['num'];
                $tem_data['goods_weight'] = $item['weight'];
                $tem_data['prom_goods'] = serialize($item['prom_goods']);
                $tem_data['spec'] = serialize($item['spec']);
                $model->table("order_goods")->data($tem_data)->insert();
            }
            //优惠券锁死
            if(!empty($voucher)){
                $model->table("voucher")->where("id=$voucher_id and user_id=".$this->user['id'])->data(array('status'=>2))->update();
            }
            //清空购物车与表单缓存
            if($order_type==0){
                $cart = Cart::getCart();
                $cart->clear();
                Session::clear("order_status");
            }
            $this->redirect("/simple/order_status/order_id/$order_id");

        }else{
            $this->redirect("login");
        }
    }
    public function order_status(){
        if($this->checkOnline()){
            $order_id = Filter::int(Req::get("order_id"));
            if($order_id){
                $order = $this->model->table("order as od")->join("left join payment as pa on od.payment= pa.id")->fields("od.id,od.order_no,od.payment,od.pay_status,od.order_amount,pa.pay_name as payname,od.type")->where("od.id=$order_id and od.status<4 and od.user_id = ".$this->user['id'])->find();
                if($order){
                    if($order['pay_status']==0){
                        $this->assign("order",$order);
                        $this->redirect();
                    }else if($order['pay_status']==1){
                        $this->redirect("/simple/order_completed/order_id/$order_id");
                    }
                }else{
                    Tiny::Msg($this,404);
                }
            }else{
                Tiny::Msg($this,404);
            }
        }else{
            $this->redirect("login");
        }
    }
    public function order_completed(){
        if($this->checkOnline()){
            $order_id = Filter::int(Req::args("order_id"));
            if($order_id){
                $order = $this->model->table("order as od")->join("left join payment as pa on od.payment= pa.id")->fields("od.id,od.order_no,od.payment,od.pay_status,od.order_amount,pa.pay_name as payname,od.type")->where("od.id=$order_id and od.status<4 and od.user_id = ".$this->user['id'])->find();
                if($order){
                    if($order['pay_status']==1){
                        $this->assign("order",$order);
                        $this->redirect();
                    }else if($order['pay_status']==0){
                        $this->redirect("/simple/order_status/order_id/$order_id");
                    }
                }else{
                    Tiny::Msg($this,404);
                }
            }else{
                Tiny::Msg($this,404);
            }
        }else{
            $this->redirect("login");
        }
    }
    public function get_voucher(){
        $page = Filter::int(Req::args("page"));
        $amount = Filter::int(Req::args("amount"));
        $where = "user_id = ".$this->user['id']." and is_send = 1";
        $where .= " and status = 0 and '".date("Y-m-d H:i:s")."' <=end_time and '".date("Y-m-d H:i:s")."' >=start_time and money<=".$amount;
        $voucher = $this->model->table("voucher")->where($where)->order("end_time")->findPage($page,10,1,true);
        $data = $voucher['data'];
        $voucher['data'] = $data;
        $voucher['status'] = "success";
        echo JSON::encode($voucher);
    }
    public function reg_result(){
        $this->assign("user",$this->user);
        $this->redirect();
    }
    public function address_other(){
        Session::set("order_status",Req::args());
        $this->layout = '';
        $id = Filter::int(Req::args("id"));
        if($id){
            $model = new Model("address");
            $data = $model->where("id = $id")->find();
            $this->redirect("address_other",false,$data);
        }
        else $this->redirect();
    }
    public function address_save(){
        $rules = array('zip:zip:邮政编码格式不正确!','addr:required:内容不能为空!','accept_name:required:收货人姓名不能为空!,mobile:mobi:手机格式不正确!,phone:phone:电话格式不正确');
        $info = Validator::check($rules);
        
        if(!is_array($info) && $info==true) {
            Filter::form(array('sql'=>'accept_name|mobile|phone','txt'=>'addr','int'=>'province|city|county|zip|is_default|id'));
            $is_default = Filter::int(Req::args("is_default"));
            if($is_default == 1){
                $this->model->table("address")->where("user_id=".$this->user['id'])->data(array('is_default'=>0))->update();
            }else{
                Req::args("is_default","0");
            }

            Req::args("user_id",$this->user['id']);
            $id = Filter::int(Req::args('id'));
            if($id){
                $this->model->table("address")->where("id=$id and user_id=".$this->user['id'])->update();
            }
            else{
                $address_id = $this->model->table("address")->insert();
                $order_status = Session::get("order_status");
                $order_status['address_id'] = $address_id;
                Session::set("order_status",$order_status);
            }
            

            $this->assign("msg",array("success","地址编辑成功!"));
            Req::args("id",null);
            //$this->redirect("address_other",false);

            echo "<script>parent.location.reload();</script>";
            exit;
        }
        else{
            $this->assign("msg",array("error",$info['msg']));
            $this->redirect("address_other",false,Req::args());
        }
    }
    public function logout(){
        $this->safebox->clear('user');
        $cookie = new Cookie();
        $cookie->setSafeCode(Tiny::app()->getSafeCode());
        $cookie->set('autologin',null,0);
        $this->redirect('login');
    }
    //检测用户是否在线
    private function checkOnline(){
        if(isset($this->user)&& $this->user['name']!=null)
            return true;
        else return false;
    }
}